﻿// <copyright file="Utility.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-10-09</date>
// <summary>Silverlight utility classes</summary>

namespace Synergist
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    /// <summary>
    /// Silverlight Utilities
    /// </summary>
    public sealed class Utility
    {
        /// <summary>
        /// Prevents a default instance of the Utility class from being created.
        /// </summary>
        private Utility() 
        { 
        }

        /// <summary>
        /// Gets a string with all of the properties that are not null.
        /// </summary>
        /// <param name="data">the object to query</param>
        /// <returns>a string formatted the property names and values</returns>
        public static string GetProperties(object data)
        {
            if (data == null)
            {
                return null;
            }

            var type = data.GetType();

            var properties = type.GetProperties().OrderBy(item => item.Name);

            StringBuilder builder = new StringBuilder("-- " + type.FullName + " --");

            foreach (var property in properties)
            {
                var value = property.GetValue(data, null);

                if (value != null)
                {
                    builder.AppendLine();
                    builder.AppendFormat(System.Globalization.CultureInfo.CurrentCulture, "{0}: {1}", property.Name, value);
                }
            }

            return builder.ToString();
        }

        /// <summary>
        /// Convets the value in an XML element to a nullable DateTime
        /// </summary>
        /// <param name="element">an XML element</param>
        /// <returns>a nullable DateTime</returns>
        public static DateTime? UnixTimeToDateTime(XElement element)
        {
            if (element == null)
            {
                return null;
            }

            var strUnixTime = element.Value;

            if (string.IsNullOrEmpty(strUnixTime))
            {
                return null;
            }

            long unixTime = long.Parse(strUnixTime, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture);

            var startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            return startTime.AddSeconds(unixTime);
        }

        /// <summary>
        /// Convert a UNIX time to a DateTime
        /// </summary>
        /// <param name="attribute">an attribute that has a UNIX time in it.</param>
        /// <returns>a DateTime</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Utility function")]
        public static DateTime UnixTimeToDateTime(XAttribute attribute)
        {
            if (attribute == null)
            {
                return DateTime.MinValue;
            }

            var strUnixTime = attribute.Value;

            if (string.IsNullOrEmpty(strUnixTime))
            {
                return DateTime.MinValue;
            }

            long unixTime = long.Parse(strUnixTime, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture);

            var startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            return startTime.AddSeconds(unixTime);
        }
    }
}
